package com.sgb.kjwl.view.ui.activity.self.packers;

import android.content.Intent;

import androidx.databinding.DataBindingUtil;

import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivitySelfMainBusinessBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.self.SelfTeamIntroduceEntity;
import com.sgb.kjwl.utils.NumberConstant;
import com.sgb.kjwl.view.ui.adapter.self.packers.FlowLayoutAdapter1;
import com.sgb.kjwl.view.widget.MyItemDecoration;
import com.sgb.kjwl.viewmodel.self.packers.SelfMainBusinessModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.FlowLayoutManager;
import com.swgk.core.util.MToast;

import java.io.Serializable;
import java.util.List;

import javax.inject.Inject;

/**
 * description:主营业务
 * @author WYJ
 * @date 2020/8/27
 */
public class SelfMainBusinessActivity extends AppActivity {

    @Inject
    SelfMainBusinessModel mModel;
    private ActivitySelfMainBusinessBinding mBind;

    private FlowLayoutAdapter1 mAdapter1;

    public static void start(AppActivity context, List<SelfTeamIntroduceEntity.BusinessScopesBean> data, int code) {
        Intent intent = new Intent(context, SelfMainBusinessActivity.class);
        intent.putExtra(AppConstant.EXTRA_ENTITY, (Serializable) data);
        context.startActivityForResult(intent, code);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        mBind = DataBindingUtil.setContentView(this, R.layout.activity_self_main_business);
        mBind.setViewModel(null);

        List<SelfTeamIntroduceEntity.BusinessScopesBean> data = (List<SelfTeamIntroduceEntity.BusinessScopesBean>) getIntent().getSerializableExtra(AppConstant.EXTRA_ENTITY);
        mBind.recycler.setLayoutManager(new FlowLayoutManager());
        mBind.recycler.addItemDecoration(new MyItemDecoration());
        mAdapter1 = new FlowLayoutAdapter1(this, data);
        mBind.recycler.setAdapter(mAdapter1);

        mBind.btnAdd.setOnClickListener(this);
        mBind.tvFinish.setOnClickListener(this);
        mBind.tvDeleteAll.setOnClickListener(this);
        mBind.ivDelete.setOnClickListener(this);
        mBind.btnRelease.setOnClickListener(this);

        mModel.getCategory("", false);
    }

    public void result(SelfTeamIntroduceEntity.BusinessScopesBean bean) {
        boolean isAdd = true;
        for (SelfTeamIntroduceEntity.BusinessScopesBean data : mAdapter1.getDatas()) {
            if (data.equals(bean)) {
                isAdd = false;
                break;
            }
        }
        if (isAdd) {
            mAdapter1.getDatas().add(bean);
            mAdapter1.notifyDataSetChanged();
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_add) {
            mModel.showPop(v);
        } else if (id == R.id.tv_delete_all) {
            mAdapter1.getDatas().clear();
            mAdapter1.notifyDataSetChanged();
        } else if (id == R.id.iv_delete) {
            mAdapter1.setDelete(true);
            mBind.deleteLayout.setVisibility(View.VISIBLE);
            mBind.ivDelete.setVisibility(View.GONE);
        } else if (id == R.id.tv_finish) {
            mAdapter1.setDelete(false);
            mBind.ivDelete.setVisibility(View.VISIBLE);
            mBind.deleteLayout.setVisibility(View.GONE);
        } else if (id == R.id.btn_release) {
            if (mAdapter1.getDatas().size() == NumberConstant.TEAM_CONSTRUCTION) {
                MToast.showToast(this, "至少选择一个主营业务");
                return;
            }
            if (mAdapter1.getDatas().size() > NumberConstant.NUMBER_TWENTY) {
                MToast.showToast(this, "主营业务最多显示20条");
                return;
            }
            setResult(RESULT_OK, new Intent().putExtra(AppConstant.EXTRA_OBJ, (Serializable) mAdapter1.getDatas()));
            finish();
        }
    }
}
